﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using SRB.Tg.Model;
using Teigha.Runtime;
using SRB.Tg.DrawingStandard;
using SRB.Tg.Cad_Bll;
using Teigha.Export_Import;
using System.Drawing;
using Teigha.Geometry;
using Teigha.GraphicsSystem;
using SRB.Tg.Cad_Bll;
using System.Collections.Specialized;
using System.IO;
using SRB.Tg.Operate;
using SRB.Tg.Common;
using SRB.Tg.Operate.Operate;
using System.Data;

namespace ExportSteelTowerStructure
{
    public class ExportSteelTowerStructure
    {
        static string appPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "CADFiles";
        static System.Data.DataTable dtQuantity;
        static System.Data.DataTable dtProjectConfig;
        static System.Data.DataTable dtCategory;

        public static string DrawingSteelTowerStructure(string bridgeId, string categoryId, string _appPath = "")
        {
            string filePath = "";
            DrawingSteelTowerStructure P1 = new DrawingSteelTowerStructure(bridgeId, categoryId, out dtCategory);
            string file = "";
            List<string> fileList = new List<string>();
            if (!string.IsNullOrWhiteSpace(_appPath))
                appPath = _appPath;
            DrawingConfigModel drawingConfigModel = new DrawingConfigModel();

           System.Data.DataTable dtAviationBridge = new System.Data.DataTable();
           System.Data.DataTable dtAviationBridgeInfo = new System.Data.DataTable();

            string sqlViBridge = "select * from vi_bridge where  id='" + bridgeId + "' or bridge_id='" + bridgeId + "'";
            dtAviationBridgeInfo = MySqlHelper.ExecuteDataTable(sqlViBridge, CommandType.Text);
            if (dtAviationBridgeInfo.Rows.Count > 0)
            {
                //string sqlSpan = "select * from vi_aviation_bridge where  bridge_id='" + dtAviationBridgeInfo.Rows[0]["bridge_id"].ToString() + "' and span_type = 'P' order by station asc";
                string sqlSpan = "select * from vi_aviation_bridge where  bridge_id='" + dtAviationBridgeInfo.Rows[0]["bridge_id"].ToString() + "' order by station asc";
                dtAviationBridge = MySqlHelper.ExecuteDataTable(sqlSpan, CommandType.Text);
      
            }
            //主梁布跨信息
            string sqlSection = "select * from vi_aviation_section where  bridge_id='" + dtAviationBridgeInfo.Rows[0]["bridge_id"].ToString() + "' ";
             System.Data.DataTable dtSection = MySqlHelper.ExecuteDataTable(sqlSection, CommandType.Text);
            double divCap = 3000,leSeg = 0;
            List<Tuple<double, double, double>> tupleLst = new List<Tuple<double, double, double>>();
            try
            {
                if (dtSection.Rows.Count > 0)
                {
                    divCap = double.Parse(dtSection.Rows[0]["segment_length"].ToString()) * 1000;
                    #region 
                    //List<double> spanLLst = new List<double>();

                    string[] spanL = { "500" };
                    if (dtSection.Rows.Count > 0)
                    {
                        string span = dtSection.Rows[0]["span_width_segment"].ToString();
                        spanL = span.Split('+');
                    }
                    for (int j = 0; j < spanL.Length; j++)
                    {

                        string[] sp = spanL[j].Split('*');
                        double lenSpan = double.Parse(sp[0]);

                        double wt = 3;
                        if (sp[1].Contains("-"))
                        {
                            string[] widthLst = sp[1].Replace("(", "").Replace(")", "").Replace("（", "").Replace("）", "").Split('-');
                            double w1 = double.Parse(widthLst[0]);
                            double w2 = double.Parse(widthLst[1]);
                            Tuple<double, double, double> tu = new Tuple<double, double, double>(lenSpan, w1, w2);
                            tupleLst.Add(tu);
                        }
                        else
                        {
                            wt = double.Parse(sp[1]);
                            Tuple<double, double, double> tu = new Tuple<double, double, double>(lenSpan, wt, wt);
                            tupleLst.Add(tu);
                        }
                    }

                    #endregion
                }              
            }
            catch
            {
                
            }
            DataRow[] drsA0 = dtAviationBridge.Select(" name='A0'");
            DataRow[] drsA1 = dtAviationBridge.Select(" name='A1'");
            double bridgeW = 4000, bridgeSlope=0.025, divHeight=3000;
            double len = 0;
            double startStation = 0;
            int numP = 0;
            int index = 0;
            if (dtAviationBridge.Rows.Count > 0)
            {
                startStation = double.Parse(dtAviationBridgeInfo.Rows[0]["start_station"].ToString()) * 100;
                bridgeSlope = double.Parse(dtAviationBridgeInfo.Rows[0]["bridge_slope"].ToString()) * 0.01;

                divCap = double.Parse(dtAviationBridgeInfo.Rows[0]["beam_section_width"].ToString()) * 1000;
                divHeight = double.Parse(dtAviationBridgeInfo.Rows[0]["beam_height"].ToString()) * 1000;
               
                double bw = divCap;
                if (tupleLst.Count > 0)
                {
                    bw = tupleLst[0].Item2 * 1000;
                    bridgeW = bw;
                }
                #region 墩
                for (int i = 0; i < dtAviationBridge.Rows.Count; i++)
                {
                    leSeg = 0;
                    string steel_tower_type = dtAviationBridge.Rows[i]["steel_tower_type"].ToString();
                    len = double.Parse(dtAviationBridge.Rows[i]["x"].ToString()) * 100 - startStation;
                    int numS = (int)(len / divCap);
                    if (drsA0.Length > 0 && i == 0)
                    {
                        len = 0;
                    }
                    else
                        len = divCap * numS + divCap * 0.5;
                  
                    string name = dtAviationBridge.Rows[i]["name"].ToString();
                    string nameLast = name;
                    if (i>1)
                       nameLast = dtAviationBridge.Rows[i-1]["name"].ToString();
                    if (name.Contains("P"))
                    {
                        if (tupleLst.Count > 0)
                        {
                            try
                            {
                                bw = tupleLst[0].Item2 * 1000;
                                for (int j = 0; j < tupleLst.Count; j++)
                                {
                                    leSeg += tupleLst[j].Item1 * 1000;
                                    if (len < leSeg)
                                    {

                                        if (j > 0)
                                            bw = tupleLst[j].Item2 * 1000 + ((len - tupleLst[j - 1].Item1 * 1000) / tupleLst[j].Item1 * 1000) * (tupleLst[j].Item3 * 1000 - tupleLst[j].Item2 * 1000);
                                        else
                                            bw = tupleLst[j].Item2 * 1000 + (len / tupleLst[j].Item1 * 1000) * (tupleLst[j].Item3 * 1000 - tupleLst[j].Item2 * 1000);
                                        break;
                                    }
                                }
                            }
                            catch { }
                            finally
                            {
                                bridgeW = bw;
                            }
                        }
                        if (steel_tower_type == "2")
                        {
                            if (numP == 0)
                            {
                                #region Y型
                                using (Services svc = new Services())
                                {
                                    SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                    SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                    HostApplicationServices.Current = new HostAppServ(svc);
                                    Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                    using (Database db = new Database())
                                    {
                                        HostApplicationServices.WorkingDatabase = db;

                                        InitConfig.InitStandardConfig(db, bridgeId);
                                        P1.DrawingSteelTowerYSeg(db, 1, bridgeId, categoryId, dtAviationBridge.Rows[i], 1, dtAviationBridge.Rows.Count + 1, out dtProjectConfig, bridgeW,bridgeSlope, divCap);
                                        drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtCategory, categoryId, dtProjectConfig);

                                        if (db != null)
                                        {
                                            creatFolder(appPath + "\\SteelTowerStructure");
                                            //file = appPath + "\\AviationBridgeGA\\桥型图.dwg";
                                            file = appPath + "\\SteelTowerStructure\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (0).ToString() + ".dwg";
                                            db.SaveAs(file, DwgVersion.vAC18);

                                        }
                                    }
                                }
                                using (Services ser = new Services())
                                {
                                    SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                    SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                    HostApplicationServices.Current = new HostAppServ(ser);
                                    Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                    //打开数据库(dwg⽂件)
                                    using (Database database = new Database(false, false))//不加参数会出错
                                    {

                                        database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                                        LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                                    }
                                }
                                #endregion
                            }
                            #region 墩构造绘制
                            using (Services svc = new Services())
                            {
                                SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                HostApplicationServices.Current = new HostAppServ(svc);
                                Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                using (Database db = new Database())
                                {
                                    HostApplicationServices.WorkingDatabase = db;

                                    InitConfig.InitStandardConfig(db, bridgeId);
                                    P1.DrawingSteelTowerCADByPier(db, bridgeId, categoryId, dtAviationBridge.Rows[i], numP + 2, dtAviationBridge.Rows.Count - drsA0.Length - drsA1.Length + 1, out dtProjectConfig, bridgeW, divCap, out index, index, nameLast);
                                    drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtCategory, categoryId, dtProjectConfig);

                                    if (db != null)
                                    {
                                        creatFolder(appPath + "\\SteelTowerStructure");
                                        //file = appPath + "\\AviationBridgeGA\\桥型图.dwg";
                                        file = appPath + "\\SteelTowerStructure\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (numP + 1).ToString() + ".dwg";
                                        db.SaveAs(file, DwgVersion.vAC18);

                                    }
                                }
                            }
                            using (Services ser = new Services())
                            {
                                SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                HostApplicationServices.Current = new HostAppServ(ser);
                                Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                //打开数据库(dwg⽂件)
                                using (Database database = new Database(false, false))//不加参数会出错
                                {

                                    database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                                    LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                                }
                            }
                            #endregion
                            numP++;

                        }
                        else
                        {
                            #region 墩构造绘制
                            using (Services svc = new Services())
                            {
                                SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                HostApplicationServices.Current = new HostAppServ(svc);
                                Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                using (Database db = new Database())
                                {
                                    HostApplicationServices.WorkingDatabase = db;

                                    InitConfig.InitStandardConfig(db, bridgeId);
                                    P1.DrawingSteelTowerByPier(db, bridgeId, categoryId, dtAviationBridge.Rows[i], numP+1, dtAviationBridge.Rows.Count - drsA0.Length - drsA1.Length, out dtProjectConfig, bridgeW, divCap, out index, index, nameLast);
                                    drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtCategory, categoryId, dtProjectConfig);

                                    if (db != null)
                                    {
                                        creatFolder(appPath + "\\SteelTowerStructure");
                                        //file = appPath + "\\AviationBridgeGA\\桥型图.dwg";
                                        file = appPath + "\\SteelTowerStructure\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (numP + 1).ToString() + ".dwg";
                                        db.SaveAs(file, DwgVersion.vAC18);

                                    }
                                }
                            }
                            using (Services ser = new Services())
                            {
                                SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                HostApplicationServices.Current = new HostAppServ(ser);
                                Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                //打开数据库(dwg⽂件)
                                using (Database database = new Database(false, false))//不加参数会出错
                                {

                                    database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                                    LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                                }
                            }
                            #endregion
                            numP++;
                        }


                    }
                }
                #endregion

                #region 合并pdf   
                FileInfo filepath = new FileInfo(file);
                string dir = filepath.DirectoryName + "\\PDF\\";
                string fileName = drawingConfigModel.drawing_no + " " + drawingConfigModel.name;
                filePath = dir + fileName + ".pdf";
                ITextPdfHelper.mergePDFFiles(fileList, filePath);
                #endregion

            }
            if (!File.Exists(filePath))
                filePath = file.Replace(".dwg", "") + ".dwg";
            return filePath;
        }
        public static string DrawingSteelTowerStructureNew(string bridgeId, string categoryId, string _appPath = "")
        {
            string filePath = "";
            DrawingSteelTowerStructure P1 = new DrawingSteelTowerStructure(bridgeId, categoryId, out dtCategory);
            string file = "";
            List<string> fileList = new List<string>();
            if (!string.IsNullOrWhiteSpace(_appPath))
                appPath = _appPath;
            DrawingConfigModel drawingConfigModel = new DrawingConfigModel();

            System.Data.DataTable dtAviationBridge = new System.Data.DataTable();
            System.Data.DataTable dtAviationBridgeInfo = new System.Data.DataTable();

            string sqlViBridge = "select * from vi_bridge where  id='" + bridgeId + "' or bridge_id='" + bridgeId + "'";
            dtAviationBridgeInfo = MySqlHelper.ExecuteDataTable(sqlViBridge, CommandType.Text);
            if (dtAviationBridgeInfo.Rows.Count > 0)
            {
                //string sqlSpan = "select * from vi_aviation_bridge where  bridge_id='" + dtAviationBridgeInfo.Rows[0]["bridge_id"].ToString() + "' and span_type = 'P' order by station asc";
                string sqlSpan = "select * from vi_aviation_bridge where  bridge_id='" + dtAviationBridgeInfo.Rows[0]["bridge_id"].ToString() + "' order by station asc";
                dtAviationBridge = MySqlHelper.ExecuteDataTable(sqlSpan, CommandType.Text);

            }
            //主梁布跨信息
            string sqlSection = "select * from vi_aviation_section where  bridge_id='" + dtAviationBridgeInfo.Rows[0]["bridge_id"].ToString() + "' ";
            System.Data.DataTable dtSection = MySqlHelper.ExecuteDataTable(sqlSection, CommandType.Text);
            double divCap = 3000, leSeg = 0;
            List<Tuple<double, double, double>> tupleLst = new List<Tuple<double, double, double>>();
            try
            {
                if (dtSection.Rows.Count > 0)
                {
                    divCap = double.Parse(dtSection.Rows[0]["segment_length"].ToString()) * 1000;
                    #region 
                    //List<double> spanLLst = new List<double>();

                    string[] spanL = { "500" };
                    if (dtSection.Rows.Count > 0)
                    {
                        string span = dtSection.Rows[0]["span_width_segment"].ToString();
                        spanL = span.Split('+');
                    }
                    for (int j = 0; j < spanL.Length; j++)
                    {

                        string[] sp = spanL[j].Split('*');
                        double lenSpan = double.Parse(sp[0]);

                        double wt = 3;
                        if (sp[1].Contains("-"))
                        {
                            string[] widthLst = sp[1].Replace("(", "").Replace(")", "").Replace("（", "").Replace("）", "").Split('-');
                            double w1 = double.Parse(widthLst[0]);
                            double w2 = double.Parse(widthLst[1]);
                            Tuple<double, double, double> tu = new Tuple<double, double, double>(lenSpan, w1, w2);
                            tupleLst.Add(tu);
                        }
                        else
                        {
                            wt = double.Parse(sp[1]);
                            Tuple<double, double, double> tu = new Tuple<double, double, double>(lenSpan, wt, wt);
                            tupleLst.Add(tu);
                        }
                    }

                    #endregion
                }
            }
            catch
            {

            }
            DataRow[] drsA0 = dtAviationBridge.Select(" name='A0'");
            DataRow[] drsA1 = dtAviationBridge.Select(" name='A1'");
            double bridgeW = 4000, bridgeSlope = 0.025, divHeight = 3000;
            double len = 0;
            double startStation = 0;
            int numP = 0;
            int index = 0;
            if (dtAviationBridge.Rows.Count > 0)
            {
                startStation = double.Parse(dtAviationBridgeInfo.Rows[0]["start_station"].ToString()) * 100;
                bridgeSlope = double.Parse(dtAviationBridgeInfo.Rows[0]["bridge_slope"].ToString()) * 0.01;

                divCap = double.Parse(dtAviationBridgeInfo.Rows[0]["beam_section_width"].ToString()) * 1000;
                divHeight = double.Parse(dtAviationBridgeInfo.Rows[0]["beam_height"].ToString()) * 1000;

                double bw = divCap;
                if (tupleLst.Count > 0)
                {
                    bw = tupleLst[0].Item2 * 1000;
                    bridgeW = bw;
                }
                #region 墩
                for (int i = 0; i < dtAviationBridge.Rows.Count; i++)
                {
                    leSeg = 0;
                    string steel_tower_type = dtAviationBridge.Rows[i]["steel_tower_type"].ToString();
                    len = double.Parse(dtAviationBridge.Rows[i]["x"].ToString()) * 100 - startStation;
                    int numS = (int)(len / divCap);
                    if (drsA0.Length > 0 && i == 0)
                    {
                        len = 0;
                    }
                    else
                        len = divCap * numS + divCap * 0.5;

                    string name = dtAviationBridge.Rows[i]["name"].ToString();
                    string nameLast = name;
                    if (i > 1)
                        nameLast = dtAviationBridge.Rows[i - 1]["name"].ToString();
                    if (name.Contains("P"))
                    {
                        if (tupleLst.Count > 0)
                        {
                            try
                            {
                                bw = tupleLst[0].Item2 * 1000;
                                for (int j = 0; j < tupleLst.Count; j++)
                                {
                                    leSeg += tupleLst[j].Item1 * 1000;
                                    if (len < leSeg)
                                    {

                                        if (j > 0)
                                            bw = tupleLst[j].Item2 * 1000 + ((len - tupleLst[j - 1].Item1 * 1000) / tupleLst[j].Item1 * 1000) * (tupleLst[j].Item3 * 1000 - tupleLst[j].Item2 * 1000);
                                        else
                                            bw = tupleLst[j].Item2 * 1000 + (len / tupleLst[j].Item1 * 1000) * (tupleLst[j].Item3 * 1000 - tupleLst[j].Item2 * 1000);
                                        break;
                                    }
                                }
                            }
                            catch { }
                            finally
                            {
                                bridgeW = bw;
                            }
                        }
                        if (steel_tower_type == "2")
                        {
                            if (numP == 0)
                            {
                                #region Y型
                                using (Services svc = new Services())
                                {
                                    SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                    SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                    HostApplicationServices.Current = new HostAppServ(svc);
                                    Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                    using (Database db = new Database())
                                    {
                                        HostApplicationServices.WorkingDatabase = db;

                                        InitConfig.InitStandardConfig(db, bridgeId);
                                        P1.DrawingSteelTowerYSeg(db, 1, bridgeId, categoryId, dtAviationBridge.Rows[i], 1, dtAviationBridge.Rows.Count + 1, out dtProjectConfig, bridgeW, bridgeSlope, divCap);
                                        drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtCategory, categoryId, dtProjectConfig);

                                        if (db != null)
                                        {
                                            creatFolder(appPath + "\\SteelTowerStructure");
                                            //file = appPath + "\\AviationBridgeGA\\桥型图.dwg";
                                            file = appPath + "\\SteelTowerStructure\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (0).ToString() + ".dwg";
                                            db.SaveAs(file, DwgVersion.vAC18);

                                        }
                                    }
                                }
                                using (Services ser = new Services())
                                {
                                    SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                    SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                    HostApplicationServices.Current = new HostAppServ(ser);
                                    Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                    //打开数据库(dwg⽂件)
                                    using (Database database = new Database(false, false))//不加参数会出错
                                    {

                                        database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                                        LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                                    }
                                }
                                #endregion
                            }
                            #region 墩构造绘制
                            using (Services svc = new Services())
                            {
                                SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                HostApplicationServices.Current = new HostAppServ(svc);
                                Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                using (Database db = new Database())
                                {
                                    HostApplicationServices.WorkingDatabase = db;

                                    InitConfig.InitStandardConfig(db, bridgeId);
                                    P1.DrawingSteelTowerCADByPier(db, bridgeId, categoryId, dtAviationBridge.Rows[i], numP + 2, dtAviationBridge.Rows.Count - drsA0.Length - drsA1.Length + 1, out dtProjectConfig, bridgeW, divCap, out index, index, nameLast);
                                    drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtCategory, categoryId, dtProjectConfig);

                                    if (db != null)
                                    {
                                        creatFolder(appPath + "\\SteelTowerStructure");
                                        //file = appPath + "\\AviationBridgeGA\\桥型图.dwg";
                                        file = appPath + "\\SteelTowerStructure\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (numP + 1).ToString() + ".dwg";
                                        db.SaveAs(file, DwgVersion.vAC18);

                                    }
                                }
                            }
                            using (Services ser = new Services())
                            {
                                SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                HostApplicationServices.Current = new HostAppServ(ser);
                                Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                //打开数据库(dwg⽂件)
                                using (Database database = new Database(false, false))//不加参数会出错
                                {

                                    database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                                    LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                                }
                            }
                            #endregion
                            numP++;

                        }
                        else
                        {
                            #region 墩构造绘制
                            using (Services svc = new Services())
                            {
                                SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                HostApplicationServices.Current = new HostAppServ(svc);
                                Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                using (Database db = new Database())
                                {
                                    HostApplicationServices.WorkingDatabase = db;

                                    InitConfig.InitStandardConfig(db, bridgeId);
                                    P1.DrawingSteelTowerByPierWithEngineeringQuantity(db, bridgeId, categoryId, dtAviationBridge.Rows[i], numP + 1, dtAviationBridge.Rows.Count - drsA0.Length - drsA1.Length, out dtProjectConfig, bridgeW, divCap, out index, index, nameLast);
                                    drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtCategory, categoryId, dtProjectConfig);

                                    if (db != null)
                                    {
                                        creatFolder(appPath + "\\SteelTowerStructure");
                                        //file = appPath + "\\AviationBridgeGA\\桥型图.dwg";
                                        file = appPath + "\\SteelTowerStructure\\" + drawingConfigModel.drawing_no + " " + drawingConfigModel.name + (numP + 1).ToString() + ".dwg";
                                        db.SaveAs(file, DwgVersion.vAC18);

                                    }
                                }
                            }
                            using (Services ser = new Services())
                            {
                                SystemObjects.DynamicLinker.LoadApp("GripPoints", false, false);
                                SystemObjects.DynamicLinker.LoadApp("PlotSettingsValidator", false, false);

                                HostApplicationServices.Current = new HostAppServ(ser);
                                Environment.SetEnvironmentVariable("DDPLOTSTYLEPATHS", ((HostAppServ)HostApplicationServices.Current).FindConfigPath(String.Format("PrinterStyleSheetDir")));
                                //打开数据库(dwg⽂件)
                                using (Database database = new Database(false, false))//不加参数会出错
                                {

                                    database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, "");
                                    LayoutTools.PlotDrawingByTK(database, "图纸", "ISO_full_bleed_A3_(420.00_x_297.00_MM)", "monochrome.ctb", "DWG To PDF.pc3", file, out filePath, ref fileList);
                                }
                            }
                            #endregion
                            numP++;
                        }


                    }
                }
                #endregion

                #region 合并pdf   
                FileInfo filepath = new FileInfo(file);
                string dir = filepath.DirectoryName + "\\PDF\\";
                string fileName = drawingConfigModel.drawing_no + " 钢塔墩构造";
                filePath = dir + fileName + ".pdf";
                ITextPdfHelper.mergePDFFiles(fileList, filePath);
                #endregion

            }
            if (!File.Exists(filePath))
                filePath = file.Replace(".dwg", "") + ".dwg";
            return filePath;
        }

     
        public static void creatFolder(String path)
        {
            //不存在 则创建
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
    }
}
